//
//  PywNewAccountApi.m
//  PywSdk
//
//  Created by Jade on 16/8/1.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywNewAccountApi.h"
#import "PywMBProgressHUD+Pyw.h"
#import "AIFAccountRegisterManager.h"
#import "PywResourceUtils.h"
#import "PropertyListReformer.h"
#import "PywIAPHelper.h"
#import "PywSdk+login.h"
#import "PywCPNotifications.h"

@interface PywNewAccountApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywNewAccountApi
/**
 *  新建游戏账号
 */
- (void)requestNewAccount:(Successd)success
                     fail:(Failure)fail
{
    self.successd = success;
    self.failure = fail;
    [PywMBProgressHUD showMessage:@"正在登录..."];
    AIFAccountRegisterManager *manager = [AIFAccountRegisterManager manager];
    manager.delegate = self;
    [manager loadData];
    
}

#pragma mark - ETAPIManagerCallBackDelegate

/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUD];
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}

/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUD];
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    if ([manager isKindOfClass:[AIFAccountRegisterManager class]]) {
        [self registerSuccess:dict];
    }
}
/**
 *  注册成功
 *
 *  @param dict <#dict description#>
 */
- (void)registerSuccess:(NSDictionary *)dict
{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    if (resultCode == 200) {
        
        if (self.successd) {
            self.successd(nil,dict);
        }
    }else {
        if (self.failure) {
            self.failure([self errorWithCode:resultCode]);
        }
    }
}
@end
